import type { PageDTO, PageVO } from '@/type/type'
import { http } from '@/request/http'
import type { LabelVO } from '@/request/api/label'
import { initLabelVO } from '@/request/api/label'
import type { MakeOptional } from '@/type'

type BaseAlbum = {
  // 主键
  id: number
  // 角色ID
  roleId: number
  // 标签ID
  labelId: number
  // 标题
  title: string
  // 路径，以,隔开
  path: string
  // 简介
  introduction: string
  // 是否启用 1 启用 0 停用
  status: boolean
}

export type AlbumDTO = MakeOptional<BaseAlbum>

export type AlbumVO = BaseAlbum & {
  // 标签
  label: LabelVO
  // 路径
  paths: string[]
  // 创建时间
  createTime: Date
}

export const initAlbumVO = (): AlbumVO => {
  return {
    id: 0,
    roleId: 0,
    labelId: 0,
    title: '',
    path: '',
    paths: [],
    introduction: '',
    createTime: new Date(),
    label: initLabelVO(),
    status: true
  }
}

export const initAlbumDTO = (): AlbumDTO => {
  return {
    id: undefined,
    roleId: undefined,
    labelId: undefined,
    title: undefined,
    path: undefined,
    introduction: undefined,
    status: undefined
  }
}

export const albumVO2DTO = (vo: AlbumVO): AlbumDTO => {
  return {
    id: vo.id,
    roleId: vo.roleId,
    labelId: vo.labelId,
    title: vo.title,
    path: vo.paths.join(','),
    introduction: vo.introduction,
    status: vo.status
  }
}

export const getAlbumApi = (id: number) => {
  return http<AlbumVO>({
    url: `/album/${id}`,
    method: 'get'
  })
}

export const getAlbumPageApi = (dto: PageDTO) => {
  return http<PageVO<AlbumVO>>({
    url: `/album/page`,
    method: 'get',
    params: {
      dto: JSON.stringify(dto)
    }
  })
}

export const getAlbumAdminPageApi = (dto: PageDTO) => {
  return http<PageVO<AlbumVO>>({
    url: `/admin/album/page`,
    method: 'get',
    params: {
      dto: JSON.stringify(dto)
    }
  })
}

export const addAlbumApi = (form: FormData) => {
  return http<boolean>({
    url: `/admin/album/insert`,
    method: 'post',
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    data: form
  })
}

export const updateAlbumApi = (data: FormData) => {
  return http<boolean>({
    url: `/admin/album/update`,
    method: 'put',
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    data: data
  })
}

export const delAlbumApi = (ids: number[]) => {
  return http<boolean>({
    url: `/admin/album/delete`,
    method: 'delete',
    data: ids
  })
}
